<template>
  <basic-container>
    <avue-crud
      :option="optionPreferentialTopUpRecord"
      :table-loading="loading"
      :data="data"
      :page="page"
      @row-del="rowDel"
      v-model="form"
      @row-update="rowUpdate"
      @row-save="rowSave"
      :before-open="beforeOpen"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @on-load="onLoad"
    >
      <template #menu="{ row, index, size }">
        <el-button
          type="text"
          icon="el-icon-document-checked"
          @click="isSetting(row)"
          >返现支付</el-button
        >
        <el-button
          type="text"
          icon="el-icon-document-checked"
          @click="isSetting(row)"
          >奖励结算</el-button
        >
      </template>
      <template #menuLeft>
        <el-form :inline="true" :model="query" class="demo-form-inline">
          <el-form-item>
            <el-date-picker
              size="small"
              v-model="searchDate"
              type="daterange"
              class="m-r-10"
              value-format="yyyy-MM-dd HH:mm:ss"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              @change="searchChange"
            />
          </el-form-item>
          <el-form-item label="返现状态：">
            <el-select
              v-model="query.returnStatus"
              placeholder="返现状态"
              size="small"
              @change="searchChange"
            >
              <el-option label="未返现" value="0"></el-option>
              <el-option label="已返现" value="1"></el-option>
              <el-option label="返现失败" value="2"></el-option>
              <el-option label="付款中" value="3"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="奖励状态：">
            <el-select
              v-model="query.region"
              placeholder="奖励状态"
              size="small"
            >
              <el-option label="未奖励" value="0"></el-option>
              <el-option label="已奖励" value="1"></el-option>
              <el-option label="奖励失败" value="2"></el-option>
              <el-option label="付款中" value="3"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              size="small"
              @click="conditionalQuery"
              :loading="loading"
              >查询</el-button
            >
          </el-form-item>
        </el-form>
      </template>
      <template #footer>
        <div class="w-50 m-t-20">
          <el-descriptions column="2" :colon="false">
            <el-descriptions-item label="充值总金额(元)"
              >kooriookami</el-descriptions-item
            >
            <el-descriptions-item label="奖励总金额(元)"
              >18100000000</el-descriptions-item
            >
            <el-descriptions-item label="赠送总金额(元)"></el-descriptions-item>
            <el-descriptions-item label="返现总金额(元)"></el-descriptions-item>
          </el-descriptions>
        </div>
      </template>
    </avue-crud>
    <el-dialog
      title="账单结算"
      append-to-body
      :visible.sync="showSettingFlag"
      width="400px"
    >
      <div class="pa-12 flex-start">
        <div style="width: 100px">结算金额</div>
      </div>
      <div class="pa-12 flex-start">
        <div style="width: 100px">备注</div>
        <el-input type="textarea"></el-input>
      </div>
      <div class="dividing-line m-t-20 m-b-20"></div>
      <div class="flex-end">
        <el-button type="primary" size="small" @click="doSetting"
          >确定</el-button
        >
        <el-button
          type="primary"
          size="small"
          plain
          @click="showSettingFlag = false"
          >取消</el-button
        >
      </div>
    </el-dialog>
  </basic-container>
</template>

<script>
import { isListEmpty } from "@/util/common-util";
import { getList } from "@/api/financial/preferential-top-up-record";
import { optionPreferentialTopUpRecord } from "@/const/financial/preferential-top-up-record";
import { mapGetters } from "vuex";

export default {
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      selectionList: [],
      optionPreferentialTopUpRecord,
      showSettingFlag: false,
      data: [],
      searchDate: null,
      optionIsSettled: [
        {
          label: "全部",
          value: undefined,
        },
        {
          label: "未结算",
          value: 0,
        },
        {
          label: "已结算",
          value: 1,
        },
      ],
    };
  },
  computed: {
    scoped() {
      return scoped;
    },
    ...mapGetters(["permission"]),
  },
  methods: {
    //账单结算
    isSetting(row) {
      this.showSettingFlag = true;
    },
    //搜索查询
    conditionalQuery() {
      if (isListEmpty(this.searchDate)) {
        this.query.startTime = "";
        this.query.endTime = "";
      } else {
        this.query.startTime = this.searchDate[0];
        this.query.endTime = this.searchDate[1];
      }
      this.onLoad(this.page, this.query);
    },
    rowSave(row, done, loading) {
      add(row).then(
        () => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(
        () => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id).then((res) => {
          this.form = res.data.data;
        });
      }
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      ).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
      });
    },
  },
};
</script>

<!--<style scoped>-->
<!--</style>-->
